import 'ref.dart';
import 'key.dart';

/// 代理某一个对象的属性
///
/// [ref] Ref实例
///
/// 会给ref实例下的每个属性值代理成RefKey对象
Map<dynamic, RefKey<T>> refKeys<T>(Ref ref) {
  /// 响应式对象集合或者响应式对象
  late Map<dynamic, RefKey<T>> _data = {};
  if (!(ref.value is List || ref.value is Map || ref.value is Set)) {
    print('app error: RefKeys 调用代理的Ref对象的值类型必须是List或者Map！');
    throw TypeError();
  } else {
    if (ref.value is List || ref.value is Set) {
      for (var i = 0; i < ref.value.length; i++) {
        _data[i] = RefKey(ref, i);
      }
    } else if (ref.value is Map) {
      ref.value.keys.forEach((key) {
        _data[key] = RefKey(ref, key);
      });
    }
  }
  return _data;
}
